import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
// import 'package:flutter_application_1/models/socket_event.dart';
// import 'package:flutter_application_1/pages/Mine/CoinStorePage.dart';
// import 'package:flutter_application_1/socket/socket_io.dart';
import 'package:path_provider/path_provider.dart';
import 'package:webview_flutter/webview_flutter.dart';
// import '../../pages/im/util/event_bus.dart';
// import '../tools/appPopPageView.dart';

class CnBaseWebView extends StatefulWidget {
  final String? url;
  final String? title;
  final bool? isShowHeader;

  const CnBaseWebView(
      {this.url, this.cookieManager, this.title, this.isShowHeader});
  final CookieManager? cookieManager;

  @override
  CnBaseWebViewState createState() => CnBaseWebViewState();
}

class CnBaseWebViewState extends State<CnBaseWebView> {
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  bool isShowHeader = true;

  @override
  void initState() {
    super.initState();
    setState(() {
      isShowHeader = widget.isShowHeader ?? false;
    });
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          '${widget.title}',
          style: TextStyle(fontSize: 18, color: Color(0xFF404040)),
          textAlign: TextAlign.center,
        ),
        centerTitle: true, //标题居中
        leading: IconButton(
            color: Colors.black,
            icon: const Icon(Icons.arrow_back_ios),
            onPressed: () => Navigator.pop(context)),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: WebView(
              initialUrl: widget.url ?? '',
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (WebViewController webViewController) {
                _controller.complete(webViewController);
              },
              onProgress: (int progress) {
                print('WebView is loading (progress : $progress%)');
              },
              javascriptChannels: <JavascriptChannel>{},
              navigationDelegate: (NavigationRequest request) {
                if (request.url.startsWith('https://www.youtube.com/')) {
                  print('blocking navigation to $request}');
                  return NavigationDecision.prevent;
                }
                print('allowing navigation to $request');
                return NavigationDecision.navigate;
              },
              onPageStarted: (String url) {
                print('Page started loading: $url');
              },
              onPageFinished: (String url) {
                print('Page finished loading: $url');
              },
              gestureNavigationEnabled: true,
              // backgroundColor: const Color(0x00000000),
            ),
          ),
        ],
      ),
    );
  }
}
